﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Reporting.WinForms;
using Billing.Reports;

namespace Billing.CashDesk
{
    [ToolboxItem(true)]
    public partial class ucPrintControl : Billing.WindowsBase.CustomControls.BaseControl
    {
        private PrintDoc printDoc;

        [Browsable(false)]
        public PrintDoc PrintDoc
        {
            get { return printDoc; }
            set
            {
                printDoc = value;
                LoadPrintDoc();
            }
        }

        public ucPrintControl()
        {
            InitializeComponent();
        }

        public ucPrintControl(PrintDoc printDoc)
            : this()
        {
            this.Load += new EventHandler(OnPrintControlLoad);

            this.printDoc = printDoc;
            LoadPrintDoc();
        }

        private void LoadPrintDoc()
        {
            if (printDoc != null && printDoc.ReporParameteres != null)
                rv.ShowParameterPrompts = printDoc.ReporParameteres.Count == 0;

            rv.ProcessingMode = ProcessingMode.Remote;
            ServerReport SR = rv.ServerReport;
            SR.ReportServerUrl = new Uri(Billing.SettingManager.getSetting("ReportService"));

            if (printDoc == null)
            {
                SR.ReportPath = string.Empty;
                return;
            }

            SR.ReportPath = printDoc.Path;
            SR.ReportServerCredentials.NetworkCredentials = new System.Net.NetworkCredential(
                        Billing.SettingManager.getSetting("ReportServiceUser"),
                        Billing.SettingManager.getSetting("ReportServicePassword"));

            SR.SetParameters(printDoc.ReporParameteres);
        }

        public void ReportRefresh()
        {
            rv.RefreshReport();
        }

        void OnPrintControlLoad(object sender, EventArgs e)
        {
            rv.RefreshReport();
        }

        public void Print()
        {
            ReportManager rm = new ReportManager();
            rm.LoadReport(printDoc.Path, printDoc.ReporParameteres);
        }
    }
}
